<?php

namespace App\Transformers;

use App\Models\Role;
use App\Models\User;
use League\Fractal\TransformerAbstract;

class UserImportTransformer extends TransformerAbstract
{
    protected   $role;

    protected $availableIncludes = ['roles'];

    public function __construct($arr='')
    {
        $role = new Role();
        $this->role = $role->getIdAndNameToArray();
    }

    public function transform(User $user)
    {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'section' => empty($user->section_id)?'':isset($this->role[$user->section_id])?$this->role[$user->section_id]:'',
            'branch' => empty($user->branch_id)?'':isset($this->role[$user->branch_id])?$this->role[$user->branch_id]:'',
            'role' => empty($user->role_id)?'':isset($this->role[$user->role_id])?$this->role[$user->role_id]:'',
            'section_id' => $user->section_id,
            'branch_id' => $user->branch_id,
            'role_id' => $user->role_id,
            'fs_user_count' => $user->fsUserTasks()->select('fs_user_id')->distinct()->get()->count(),
            'created_at' => empty($user->created_at)?'':$user->created_at->toDateTimeString(),
            'updated_at' => empty($user->updated_at)?'':$user->updated_at->toDateTimeString(),
        ];
    }


    public function includeRoles(User $user)
    {
        return $this->collection($user->roles->where('is_role',true),new RoleTransformer());
    }
}